//
//  DWBaseData.swift
//  DWater-Swift
//
//  Created by mac on 2017/4/20.
//  Copyright © 2017年 Dimon.胡亚宾. All rights reserved.
//

import UIKit

class DWBaseData: NSObject {

 
    
    /// 加密特殊
    class   func encryptionData( dic:Dictionary<String, Any>!) -> String! {
       
        let dics        = self.BaseDictionary(dics: dic)
        print(dics)
        let jsonString  = self.toolsChangeToJson(info: dics)
        let key         = defaults.object(forKey: "kmd516")
        return SecurityUtil.encryptAESData(jsonString, key: key as! String!)
        
    }
    
    class   func encryptionDataBackDic( dic:Dictionary<String, Any>?) -> Dictionary<String, Any>! {
        guard let _ = dic else {
            let paramss = Dictionary<String, Any>()
            let dics        = self.BaseDictionary(dics: paramss)
            let jsonString  = self.toolsChangeToJson(info: dics)
            let key         = defaults.object(forKey: "kmd516")
            
            var params : [String:Any] = Dictionary()
            params["yhzh"] = DWUserManager.shared.userInfo?.phoneNumeber
            params["infoMap"] = SecurityUtil.encryptAESData(jsonString, key: key as! String!)
            return params
        }
        
        let dics        = self.BaseDictionary(dics: dic!)
        let jsonString  = self.toolsChangeToJson(info: dics)
        let key         = defaults.object(forKey: "kmd516")
        
        var params : [String:Any] = Dictionary()
        params["yhzh"] = DWUserManager.shared.userInfo?.phoneNumeber
        params["infoMap"] = SecurityUtil.encryptAESData(jsonString, key: key as! String!)
        
        return params
        
    }
    
    
    //普通加密
    class   func commonlyDictionary(dic:Dictionary<String, Any>!) -> String! {
    
        let jsonString  = self.toolsChangeToJson(info: dic)
        let key         = defaults.object(forKey: "kmd516")
        return SecurityUtil.encryptAESData(jsonString, key: key as! String!)
    
    }
   
    open  class  func BaseDictionary(dics:Dictionary<String, Any>) -> Dictionary<String, Any> {
     
           var dic  = dics
        guard  let _  = DWUserManager.shared.userInfo?.userId,
               let _  = DWUserManager.shared.userInfo?.token
            else {
                dic["sblx"]     = 4
                dic["xtlx"]     = 0
                
            return dic
        }
        
        
        let userId      = DWUserManager.shared.userInfo?.userId
        let userToken   = DWUserManager.shared.userInfo?.token
        dic["yhid"]     = userId!
        dic["token"]    = userToken
        dic["sblx"]     = 4
        dic["xtlx"]     = 0
        
        return dic
    }
    
    
  open class func toolsChangeToJson(info: Any) -> String{
        //首先判断能不能转换
        guard JSONSerialization.isValidJSONObject(info) else {
            print("json转换失败")
            return ""
        }
    
        let jsonData = try? JSONSerialization.data(withJSONObject: info, options: [])
        if let jsonData = jsonData {
            let str = String(data: jsonData, encoding: String.Encoding.utf8)
            return str ?? ""
        }else {
            return ""
        }
    }
    
    
    
}
